package game.actions;

import edu.monash.fit2099.engine.actions.Action;
import edu.monash.fit2099.engine.actors.Actor;
import edu.monash.fit2099.engine.actors.attributes.BaseAttributes;
import edu.monash.fit2099.engine.positions.GameMap;
import game.actors.Player;

import java.util.concurrent.ThreadLocalRandom;

/**
 * Multi-turn sleeping action (6-10 turns).
 * While this action is returning itself via getNextAction(), Player.playTurn()
 * will not apply hydration/warmth decay (because the engine resumes this action directly).
 */
public class SleepAction extends Action {

    private int turnsLeft = -1; // set on first execute

    @Override
    public String execute(Actor actor, GameMap map) {
        if (!(actor instanceof Player)) {
            return actor + " cannot sleep.";
        }
        Player p = (Player) actor;

        // On first call, roll 6..10 inclusive
        if (turnsLeft < 0) {
            turnsLeft = ThreadLocalRandom.current().nextInt(6, 11);
        }

        // Consume a sleep turn
        turnsLeft--;

        // Status line while sleeping (Hydration/Warmth are NOT decreasing during sleep)
        String status = String.format(
                "%s sleeps... (%d turns remaining)  [HP %d/%d, Hydration %d/%d, Warmth %d/%d]",
                actor,
                Math.max(turnsLeft, 0),
                p.getAttribute(BaseAttributes.HEALTH),
                p.getMaximumAttribute(BaseAttributes.HEALTH),
                p.getHydration(),
                p.getMaxHydration(),
                p.getWarmth(),
                p.getMaxWarmth()
        );

        return status;
    }

    @Override
    public String menuDescription(Actor actor) {
        return "Sleep on bedroll (skip 6-10 turns)";
    }

    @Override
    public Action getNextAction() {
        // Keep sleeping until no turns remain
        return (turnsLeft > 0) ? this : null;
    }
}
